package com.uchengwang.ui.cm_ui.uu.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.litesuits.orm.db.model.ColumnsValue;
import com.uchengwang.R;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.BaseNoTitleActivity;
import com.uchengwang.ui.cm_ui.uu.adapter.ChooseMoodAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.ChooseMoodBean;
import com.uchengwang.ui.cm_ui.uu.bean.XiangliaoBean;
import com.uchengwang.ui.cm_ui.uu.http.XiangliaoManager;
import com.uchengwang.ui.listener.PermissionsListener;
import com.uchengwang.ui.utils.MapManager;
import com.uchengwang.ui.utils.PermissionUtils;
import com.uchengwang.ui.utils.StatusBarUtil;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/4/17.
 * 享聊选择心情
 */

public class XiangliaoChooseMoodActivity extends BaseNoTitleActivity {
    private ImageView gif;
    private ImageView chooseMood_left;
    private GridView gv;
    private ChooseMoodAdapter adapter;

    private List<ChooseMoodBean> moodBeanList;
    private double lng, lat;//经度, 纬度
    private XiangliaoBean xiangliaoBean;
    private int emotionType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StatusBarUtil.getUtil().setTransparencyBar(this);
        StatusBarUtil.getUtil().clearStatusBarDarkMode(this);
        setContentView(R.layout.activity_xiangliao_choose_mood);


        initView();
    }

    private void initView(){
        gif = (ImageView) findViewById(R.id.chooseMood_gif);
        chooseMood_left = (ImageView) findViewById(R.id.chooseMood_left);
        gv = (GridView) findViewById(R.id.chooseMood_gv);

        chooseMood_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XiangliaoChooseMoodActivity.this.finish();
            }
        });

//        gif.setBackgroundResource(R.drawable.gif_want_chat);
        ImageManager.getInstance().setRoundSize(0).loadImage(this, gif, R.drawable.image_xiangliao_bg, ImageManager.ImageType.ROUND);


        initData();
        checkLocationPermission();
    }

    private void initData(){
        moodBeanList = new ArrayList<>();
        moodBeanList.add(new ChooseMoodBean(1, R.drawable.icon_1xiangliao_orange_no, "橙色兴奋"));
        moodBeanList.add(new ChooseMoodBean(2, R.drawable.icon_2xiangliao_blue_no, "蓝色向往"));
        moodBeanList.add(new ChooseMoodBean(3, R.drawable.icon_3xiangliao_red_no, "红色愤怒"));
        moodBeanList.add(new ChooseMoodBean(4, R.drawable.icon_4xiangliao_green_no, "绿色感悟"));
        moodBeanList.add(new ChooseMoodBean(5, R.drawable.icon_5xiangliao_zise_no, "紫色压力"));
        moodBeanList.add(new ChooseMoodBean(6, R.drawable.icon_6xiangliao_huise_no, "灰色悲伤"));
        moodBeanList.add(new ChooseMoodBean(7, R.drawable.icon_7xiangliao_qingse_no, "青色思念"));

        adapter = new ChooseMoodAdapter(this, moodBeanList);
        gv.setAdapter(adapter);


        List<XiangliaoBean> beanList = LiteOrmDBUtil.getLiteOrm(this).getQueryByWhere(XiangliaoBean.class, "uid", String.valueOf(UCommonUtil.getUtil().getId()));

        if (beanList != null && beanList.size() >0){
            xiangliaoBean = beanList.get(0);
        }

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                emotionType = moodBeanList.get(position).getMoodType();

                if (lng<= 0|| lat <=0){
                    checkLocationPermission();
                }else {
                    post();
                }
            }
        });

    }

    private void checkLocationPermission(){
        PermissionUtils.getUtils().checkLocation(this, new PermissionsListener() {
            @Override
            public void onSuccess() {
                getCurrentLocation();
            }

            @Override
            public void onFailed() {
                UCommonUtil.getUtil().showToast(XiangliaoChooseMoodActivity.this, "请授权获取位置的权限");
            }
        });

    }

    /**
     * 获取当前位置
     * */
    private void getCurrentLocation(){
        MapManager.getMapManager().startLocation(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                Log.d("ucw", "定位="+aMapLocation.toString());
                if (aMapLocation != null){
                    lng = aMapLocation.getLongitude();
                    lat = aMapLocation.getLatitude();

                    if (emotionType >0){
                        post();
                    }

                }
            }
        });
    }

    //提交网络
    private void post(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.LNG,lng);
        map.put(BaseConfig.LAT, lat);
        map.put(BaseConfig.EMOTION_TYPE, emotionType);
        XiangliaoManager.getManager().release(this, map, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (stutas ==0){
                    if (xiangliaoBean != null){
                        //说明之前有这个数据  更新
                        ColumnsValue value = new ColumnsValue(new String[]{"createTime", "emotionType"}, new Object[]{System.currentTimeMillis(), emotionType});
                        LiteOrmDBUtil.getLiteOrm(XiangliaoChooseMoodActivity.this).update(XiangliaoBean.class, value, "uid", xiangliaoBean.getUid());
                    }else {
                        //插入一条数据
                        xiangliaoBean = new XiangliaoBean();
                        xiangliaoBean.setCreateTime(System.currentTimeMillis());
                        xiangliaoBean.setEmotionType(emotionType);
                        xiangliaoBean.setUid(UCommonUtil.getUtil().getId());
                        LiteOrmDBUtil.getLiteOrm(XiangliaoChooseMoodActivity.this).insert(xiangliaoBean);
                    }
//                    SPUtils.getPreferences(XiangliaoChooseMoodActivity.this).setEnterEnjoyChatTime();

                    Intent intent = new Intent(XiangliaoChooseMoodActivity.this, XiangliaoActivity.class);
                    startActivity(intent);
                    XiangliaoChooseMoodActivity.this.finish();
                }else {
                    UCommonUtil.getUtil().showToast(XiangliaoChooseMoodActivity.this, "请重试");
                }
            }
        });


    }

    @Override
    protected void onStop() {
        super.onStop();
        MapManager.getMapManager().stopLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MapManager.getMapManager().desLocation();
    }
}
